﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_FeeBGTruePayInput : DockContent
    {
        private DGBY.BLL.FeeSJTruePayTab m_bllFeeSJTruePayTab = new DGBY.BLL.FeeSJTruePayTab();

        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.FeeBGTruePayTab m_bllFeeBGTruePayTab = new DGBY.BLL.FeeBGTruePayTab();
        private int m_nFeeBGTruePayTabId = 0;
        private readonly DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        DataSet m_dsBGTruePay = null;
        private int m_nSelectIndex = 0;
        private DGBY.BLL.BGHarborTab m_bllBGHarborTab = new DGBY.BLL.BGHarborTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();

        private DataSet m_dsPaidan = new DataSet();

        public Frm_FeeBGTruePayInput()
        {
            InitializeComponent();
        }

        private void InitCombox()
        {
            m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndID("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

                this.comboBoxContractNo.SelectedIndex = 0;
            }
        }

        private void Frm_FeeBGTruePayInput_Load(object sender, EventArgs e)
        {
            ultraGrid1.Visible = false;

            InitCombox();
        }
        
        private void ShowFeeBGTruePayTabInfo(DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab)
        {
            this.textBoxTGD.Text = modelFeeBGTruePayTab.TGDFee.ToString();
            this.textBoxBG.Text = modelFeeBGTruePayTab.BGFee.ToString();
            this.textBoxLG.Text = modelFeeBGTruePayTab.LGFee.ToString();

            this.textBoxGKJS.Text = modelFeeBGTruePayTab.GKJSFee.ToString();
            this.textBoxGC.Text = modelFeeBGTruePayTab.GCFee.ToString();
            this.textBoxCG.Text = modelFeeBGTruePayTab.CGFee.ToString();

            this.textBoxGPS.Text = modelFeeBGTruePayTab.KAFee.ToString();
            this.textBoxJY.Text = modelFeeBGTruePayTab.JYFee.ToString();
            this.textBoxQT.Text = modelFeeBGTruePayTab.OtherFee.ToString();

            this.textBoxCS.Text = modelFeeBGTruePayTab.CSFee.ToString();
            this.textBoxSM.Text = modelFeeBGTruePayTab.SMFee.ToString();




            this.textBoxTGDRecv.Text = modelFeeBGTruePayTab.BGTGDFee.ToString();
            this.textBoxBGRecv.Text = modelFeeBGTruePayTab.BGBGFee.ToString();
            this.textBoxLGRecv.Text = modelFeeBGTruePayTab.BGLGFee.ToString();

            this.textBoxGKJSRecv.Text = modelFeeBGTruePayTab.BGGKJSFee.ToString();
            this.textBoxGCRecv.Text = modelFeeBGTruePayTab.BGGCFee.ToString();
            this.textBoxCGRecv.Text = modelFeeBGTruePayTab.BGCGFee.ToString();

            this.textBoxGPSRecv.Text = modelFeeBGTruePayTab.BGKAFee.ToString();
            this.textBoxJYRecv.Text = modelFeeBGTruePayTab.BGJYFee.ToString();
            this.textBoxQTRecv.Text = modelFeeBGTruePayTab.BGOtherFee.ToString();

            this.textBoxCSRecv.Text = modelFeeBGTruePayTab.BGCSFee.ToString();
            this.textBoxSMRecv.Text = modelFeeBGTruePayTab.BGSMFee.ToString();



            this.textBoxJingBan.Text = modelFeeBGTruePayTab.JinBanPerson;
            this.textBoxRecvPersonS.Text = modelFeeBGTruePayTab.RecvMonyePerson;

            this.textBoxJTNY.Text = modelFeeBGTruePayTab.TruePayMonth.ToString();
            this.textBoxRecvNY.Text = modelFeeBGTruePayTab.ShouldPayMonth.ToString();
            this.textBoxNote.Text = modelFeeBGTruePayTab.Note;

            this.textFhcgfBox.Text = modelFeeBGTruePayTab.FHCGFee.ToString();
            this.textRcddfBox.Text = modelFeeBGTruePayTab.RCDDFee.ToString();

            this.textBGFhcgfBox.Text = modelFeeBGTruePayTab.BGFHCGFee.ToString();
            this.textBGRcddfBox.Text = modelFeeBGTruePayTab.BGRCDDFee.ToString();
            
            this.checkBoxCheckJinE.Checked = modelFeeBGTruePayTab.isChaGui;
            this.checkBoxZJInvoice.Checked = modelFeeBGTruePayTab.bitFiledReserv1;
        }

        private void UpdateControl()
        {
            try
            {

                string str = this.comboBoxContractNo.SelectedValue.ToString();

                int nID = Convert.ToInt32(str, 10);

                m_dsBGTruePay = m_bllFeeBGTruePayTab.GetList("FeeBGTruePayTab.PaidanRegisterTabID = '" + nID + "'", "1=0");
                if (m_dsBGTruePay.Tables.Count > 0 && m_dsBGTruePay.Tables[0].Rows.Count > 0)
                {
                    DisplayTraGridList(m_dsBGTruePay);
                    DisplaySelectTraGridValue(m_dsBGTruePay, m_nSelectIndex);   
                }

            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }

        private void DisplayTraGridList(DataSet ds)
        {
            ultraGrid1.DataSource = null;
            if (ds.Tables[0].Rows.Count > 1)
            {
                ultraGrid1.Visible = true;
                ultraGrid1.DataSource = ds.Tables[0];
                for (int i = 0; i < ultraGrid1.DisplayLayout.Bands[0].Columns.Count; i++)
                    ultraGrid1.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Hidden = false;


                //ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;

                //ultraGrid1.DisplayLayout.Bands[0].Columns["编号是否手动生成"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["实付日期"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["是否直接开票"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["录入日期"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["申报年月"].Hidden = true;

                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收通关单费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收报关费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收港口建设费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收GPS费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收连柜费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收加页费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收查柜费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收改船费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收扫描费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收茶水费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收放行后查柜费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收入仓代垫费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收其他费用"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额"].Hidden = true;

                //ultraGrid1.DisplayLayout.Bands[0].Columns["通关单费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["报关费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["港口建设费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["GPS费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["连柜费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["加页费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["查柜费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["改船费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["扫描费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["茶水费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["放行后查柜费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["入仓代垫费"].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns["其他费用"].Hidden = true;
            }
            else
            {
                ultraGrid1.Visible = false;
            }
        }

       private void DisplaySelectTraGridValue(DataSet ds, int nIndex)
       {
           if (ds.Tables[0].Rows.Count - 1 < nIndex)
               return;

           m_nFeeBGTruePayTabId = Convert.ToInt32(ds.Tables[0].Rows[nIndex]["FeeID"].ToString(), 10);
           DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab = m_bllFeeBGTruePayTab.GetModel(m_nFeeBGTruePayTabId);
           ShowFeeBGTruePayTabInfo(modelFeeBGTruePayTab);

           this.textBoxPayG.Text = ds.Tables[0].Rows[nIndex]["对公实付金额"].ToString();
           this.textBoxPayS.Text = ds.Tables[0].Rows[nIndex]["对私实付金额"].ToString();
           this.textBoxPayTotal.Text = ds.Tables[0].Rows[nIndex]["应付实付金额"].ToString();
           this.textBoxRecvTotal.Text = ds.Tables[0].Rows[nIndex]["应收金额"].ToString();
           this.textBoxComp.Text = ds.Tables[0].Rows[nIndex]["经营单位"].ToString();

           this.textBoxDep.Text = ds.Tables[0].Rows[nIndex]["部门"].ToString();
           this.textBoxFac.Text = ds.Tables[0].Rows[nIndex]["工厂简称"].ToString();
           this.textBoxFeeFac.Text = ds.Tables[0].Rows[nIndex]["收费工厂"].ToString();
           this.textBoxGenDanPerson.Text = ds.Tables[0].Rows[nIndex]["跟单人员"].ToString();
           this.textBoxHXDNo.Text = ds.Tables[0].Rows[nIndex]["核销单号"].ToString();

           int nBoxBGH = Convert.ToInt32(ds.Tables[0].Rows[nIndex]["转关报关行"].ToString());
           if (33 == nBoxBGH)
           {
               this.textBoxBGH.Text = ds.Tables[0].Rows[nIndex]["报关行"].ToString();
           }
           else
           {
               DataSet dsTemp = m_bllBGHDataTab.GetList("BGHDataTabID = '" + nBoxBGH.ToString() + "'");
               if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
               {
                   this.textBoxBGH.Text = dsTemp.Tables[0].Rows[0]["报关行简称"].ToString();
               }
           }

           int nBoxBGG = Convert.ToInt32(ds.Tables[0].Rows[nIndex]["转关报关港"].ToString());
           if (10 == nBoxBGG)
           {
               this.textBoxBGG.Text = ds.Tables[0].Rows[nIndex]["报关港"].ToString();
           }
           else
           {
               DataSet dsTemp = m_bllBGHarborTab.GetList("BGHarborTabID = '" + nBoxBGG.ToString() + "'");
               if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
               {
                   this.textBoxBGG.Text = dsTemp.Tables[0].Rows[0]["报关港"].ToString();
               }
           }

           this.textBoxRecvPersonG.Text = ds.Tables[0].Rows[nIndex]["对公收款人"].ToString();
           this.richTextBoxGuiNo.Text = ds.Tables[0].Rows[nIndex]["集装箱柜号"].ToString();
           this.textBoxFeeNoG.Text = ds.Tables[0].Rows[nIndex]["对公费用编号"].ToString();

           this.textBoxFeeNoS.Text = ds.Tables[0].Rows[nIndex]["对私费用编号"].ToString();

           this.dateTimePickerSB.Value = ds.Tables[0].Rows[nIndex]["申报日期"].ToString() == "" ? DateTime.Parse("1900-01-01") : (DateTime)ds.Tables[0].Rows[nIndex]["申报日期"];
           this.dateTimePickerTruePay.Value = ds.Tables[0].Rows[nIndex]["实付日期"].ToString() == "" ? DateTime.Parse("1900-01-01") : (DateTime)ds.Tables[0].Rows[nIndex]["实付日期"];
           this.dateTimePickerInput.Value = ds.Tables[0].Rows[nIndex]["录入日期"].ToString() == "" ? DateTime.Parse("1900-01-01") : (DateTime)ds.Tables[0].Rows[nIndex]["录入日期"];
           this.textBoxSBNY.Text = ds.Tables[0].Rows[nIndex]["申报年月"].ToString();
           this.checkBoxAuto.Checked = (bool)ds.Tables[0].Rows[nIndex]["编号是否手动生成"];

           this.textFhcgfBox.Text = ds.Tables[0].Rows[nIndex]["放行后查柜费"].ToString();
           this.textRcddfBox.Text = ds.Tables[0].Rows[nIndex]["入仓代垫费"].ToString();

           this.textBGFhcgfBox.Text = ds.Tables[0].Rows[nIndex]["应收放行后查柜费"].ToString();
           this.textBGRcddfBox.Text = ds.Tables[0].Rows[nIndex]["应收入仓代垫费"].ToString();

           DataSet dsSjGuiCount = m_bllFeeBGTruePayTab.GetFacGoodsNameAndSjGuiCountByPaidanID(modelFeeBGTruePayTab.PaidanRegisterTabID);
           if (dsSjGuiCount.Tables.Count > 0 && dsSjGuiCount.Tables[0].Rows.Count > 0)
           {
               if ("家具" == dsSjGuiCount.Tables[0].Rows[0]["货品种类"].ToString())
               {
                   int nSJGuiCount = Convert.ToInt32(dsSjGuiCount.Tables[0].Rows[0]["商检柜数"].ToString());
                   if (nSJGuiCount > 0)
                   {
                       this.labelSJIlls.Text = "有做商检";
                   }
                   else
                   {
                       this.labelSJIlls.Text = "不用做商检";
                   }
               }
               else
               {
                   this.labelSJIlls.Text = "";
               }
           }
           else
           {
               this.labelSJIlls.Text = "";
           }
       }

        private void UpdateControl(int nFeeBGTruePayTabID)
        {
            try
            {

                //string str = this.comboBoxContractNo.SelectedValue.ToString();

                //int nID = Convert.ToInt32(str, 10);

                DataSet ds = m_bllFeeBGTruePayTab.GetList("FeeBGTruePayTab.FeeBGTruePayTabID = '" + nFeeBGTruePayTabID + "'", "1=0");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    m_nFeeBGTruePayTabId = Convert.ToInt32(ds.Tables[0].Rows[0]["FeeID"].ToString(), 10);
                    DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab = m_bllFeeBGTruePayTab.GetModel(m_nFeeBGTruePayTabId);
                    ShowFeeBGTruePayTabInfo(modelFeeBGTruePayTab);

                    this.textBoxPayG.Text = ds.Tables[0].Rows[0]["对公实付金额"].ToString();
                    this.textBoxPayS.Text = ds.Tables[0].Rows[0]["对私实付金额"].ToString();
                    this.textBoxPayTotal.Text = ds.Tables[0].Rows[0]["应付实付金额"].ToString();
                    this.textBoxRecvTotal.Text = ds.Tables[0].Rows[0]["应收金额"].ToString();
                    this.textBoxComp.Text = ds.Tables[0].Rows[0]["经营单位"].ToString();

                    this.textBoxDep.Text = ds.Tables[0].Rows[0]["部门"].ToString();
                    this.textBoxFac.Text = ds.Tables[0].Rows[0]["工厂简称"].ToString();
                    this.textBoxFeeFac.Text = ds.Tables[0].Rows[0]["收费工厂"].ToString();
                    this.textBoxGenDanPerson.Text = ds.Tables[0].Rows[0]["跟单人员"].ToString();
                    this.textBoxHXDNo.Text = ds.Tables[0].Rows[0]["核销单号"].ToString();

                    this.textBoxBGH.Text = ds.Tables[0].Rows[0]["报关行"].ToString();
                    this.textBoxBGG.Text = ds.Tables[0].Rows[0]["报关港"].ToString();
                    this.textBoxRecvPersonG.Text = ds.Tables[0].Rows[0]["对公收款人"].ToString();
                    this.richTextBoxGuiNo.Text = ds.Tables[0].Rows[0]["集装箱柜号"].ToString();
                    this.textBoxFeeNoG.Text = ds.Tables[0].Rows[0]["对公费用编号"].ToString();

                    this.textBoxFeeNoS.Text = ds.Tables[0].Rows[0]["对私费用编号"].ToString();

                    this.dateTimePickerSB.Value = ds.Tables[0].Rows[0]["申报日期"].ToString() == "" ? DateTime.Parse("1900-01-01") : (DateTime)ds.Tables[0].Rows[0]["申报日期"];
                    this.dateTimePickerTruePay.Value = ds.Tables[0].Rows[0]["实付日期"].ToString() == "" ? DateTime.Parse("1900-01-01") : (DateTime)ds.Tables[0].Rows[0]["实付日期"];
                    this.dateTimePickerInput.Value = ds.Tables[0].Rows[0]["录入日期"].ToString() == "" ? DateTime.Parse("1900-01-01") : (DateTime)ds.Tables[0].Rows[0]["录入日期"];
                    this.textBoxSBNY.Text = ds.Tables[0].Rows[0]["申报年月"].ToString();
                    this.checkBoxAuto.Checked = (bool)ds.Tables[0].Rows[0]["编号是否手动生成"];
                    this.textFhcgfBox.Text = ds.Tables[0].Rows[0]["应付放行后查柜费"].ToString();
                    this.textRcddfBox.Text = ds.Tables[0].Rows[0]["应付入仓代垫费"].ToString();

                    this.textBGFhcgfBox.Text = ds.Tables[0].Rows[0]["应收放行后查柜费"].ToString();
                    this.textBGRcddfBox.Text = ds.Tables[0].Rows[0]["应收入仓代垫费"].ToString();
                }

            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }

        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_nSelectIndex = 0;
                ProcessBGFee((int)this.comboBoxContractNo.SelectedValue, this.comboBoxContractNo.Text);
                UpdateControl();

            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
 
        }
        private void ProcessBGFee(int nPaidanRegisterTabID, string strContractNo)
        {
            int nIsSync = 0;
            int nFeeBGTruePayTabID = 0;
            DataSet ds = m_bllFeeBGTruePayTab.GetList("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'", "1=0");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nIsSync = (int)ds.Tables[0].Rows[0]["是否已同步费用"];
                nFeeBGTruePayTabID = (int)ds.Tables[0].Rows[0]["FeeID"];
            }

            DataSet dsTemp = m_bllBGCKHTTab.ShowAllCKHTMX(strContractNo);
            int nItemCount = dsTemp.Tables[0].Rows.Count;
            DataSet dsInfo = m_bllBGCKHTTab.ShowContractInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");

            if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
            {
                int nGuiCount = (int)dsInfo.Tables[0].Rows[0]["柜数"];
                if (nFeeBGTruePayTabID != 0 && nIsSync == 0)
                {
                    bool bRet = IsSJ(nPaidanRegisterTabID);
                    DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
   = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);

                    modelFeeBGTruePayTabOld.BGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付报关费定价"];
                    modelFeeBGTruePayTabOld.LGFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应付连柜费定价"]) * (nGuiCount - 1);
                    if (bRet)
                    {
                        modelFeeBGTruePayTabOld.TGDFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付通关单费定价"];
                    }
          
                    modelFeeBGTruePayTabOld.JYFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应付加页费定价"]) * ((nItemCount - 1) / 5);

                    modelFeeBGTruePayTabOld.BGBGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收报关费定价"];
                    modelFeeBGTruePayTabOld.BGLGFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应收连柜费定价"]) * (nGuiCount - 1);
                    if (bRet)
                    {
                        modelFeeBGTruePayTabOld.BGTGDFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收通关单费定价"];
                    }

                    modelFeeBGTruePayTabOld.BGGKJSFee = 0;
                    modelFeeBGTruePayTabOld.GKJSFee = 0;
                    //for (int i = 0; i < dsInfo.Tables[0].Rows.Count; ++i)
                    for (int i = 0; i < nGuiCount; ++i)
                    {
                        modelFeeBGTruePayTabOld.BGGKJSFee += (Decimal)dsInfo.Tables[0].Rows[i]["应收港口建设费定价"];
                        modelFeeBGTruePayTabOld.GKJSFee += (Decimal)dsInfo.Tables[0].Rows[i]["应付港口建设费定价"];
                    }
 
                    modelFeeBGTruePayTabOld.BGJYFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应收加页费定价"]) * ((nItemCount - 1) / 5);

                    modelFeeBGTruePayTabOld.IsSync = 1;
                    m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTabOld);
                }

            }
        }

        private bool IsSJ(int nPaidanRegisterTabID)
        {
            bool bRet = false;
            DataSet ds = m_bllFeeSJTruePayTab.GetListInfo("FeeSJTruePayTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "' and FactoryDataTabID != 6");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                bRet = true;
               
            }
            return bRet;
        }

        private void ProcessBGFeeEx(int nPaidanRegisterTabID, string strContractNo)
        {
            //int nIsSync = 0;
            int nFeeBGTruePayTabID = 0;
            DataSet ds = m_bllFeeBGTruePayTab.GetList("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'", "1=0");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                //nIsSync = (int)ds.Tables[0].Rows[0]["是否已同步费用"];
                nFeeBGTruePayTabID = (int)ds.Tables[0].Rows[0]["FeeID"];
            }

            DataSet dsTemp = m_bllBGCKHTTab.ShowAllCKHTMX(strContractNo);
            int nItemCount = dsTemp.Tables[0].Rows.Count;
            DataSet dsInfo = m_bllBGCKHTTab.ShowContractInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");

            if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
            {
                int nGuiCount = (int)dsInfo.Tables[0].Rows[0]["柜数"];
                if (nFeeBGTruePayTabID != 0)
                {
                    bool bRet = IsSJ(nPaidanRegisterTabID);
                    DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
   = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);

                    modelFeeBGTruePayTabOld.BGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付报关费定价"];
                    modelFeeBGTruePayTabOld.LGFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应付连柜费定价"]) * (nGuiCount - 1);
                    if (bRet)
                    {
                        modelFeeBGTruePayTabOld.TGDFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付通关单费定价"];
                    }
                    
                    modelFeeBGTruePayTabOld.JYFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应付加页费定价"]) * ((nItemCount - 1) / 5);

                    modelFeeBGTruePayTabOld.BGBGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收报关费定价"];
                    modelFeeBGTruePayTabOld.BGLGFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应收连柜费定价"]) * (nGuiCount - 1);
                    if (bRet)
                    {
                        modelFeeBGTruePayTabOld.BGTGDFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收通关单费定价"];
                    }

                    modelFeeBGTruePayTabOld.BGGKJSFee = 0;
                    modelFeeBGTruePayTabOld.GKJSFee = 0;
                    //for (int i = 0; i < dsInfo.Tables[0].Rows.Count; ++i)
                    for (int i = 0; i < nGuiCount; ++i)
                    {
                        modelFeeBGTruePayTabOld.BGGKJSFee += (Decimal)dsInfo.Tables[0].Rows[i]["应收港口建设费定价"];
                        modelFeeBGTruePayTabOld.GKJSFee += (Decimal)dsInfo.Tables[0].Rows[i]["应付港口建设费定价"];
                    }
                    modelFeeBGTruePayTabOld.BGJYFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应收加页费定价"]) * ((nItemCount - 1) / 5);

                    //modelFeeBGTruePayTabOld.IsSync = 1;
                    m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTabOld);
                    Frm_Main.Log2DB(this.Text, "重新计算", strContractNo);
                }

            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab = m_bllFeeBGTruePayTab.GetModel(m_nFeeBGTruePayTabId);
            modelFeeBGTruePayTab.Note = this.textBoxNote.Text;
            modelFeeBGTruePayTab.TGDFee = Convert.ToDecimal(this.textBoxTGD.Text);
            modelFeeBGTruePayTab.BGFee = Convert.ToDecimal(this.textBoxBG.Text);
            modelFeeBGTruePayTab.LGFee = Convert.ToDecimal(this.textBoxLG.Text);
            modelFeeBGTruePayTab.GKJSFee = Convert.ToDecimal(this.textBoxGKJS.Text);
            modelFeeBGTruePayTab.GCFee = Convert.ToDecimal(this.textBoxGC.Text);
            modelFeeBGTruePayTab.CGFee = Convert.ToDecimal(this.textBoxCG.Text);
            modelFeeBGTruePayTab.KAFee = Convert.ToDecimal(this.textBoxGPS.Text);
            modelFeeBGTruePayTab.JYFee = Convert.ToDecimal(this.textBoxJY.Text);
            modelFeeBGTruePayTab.OtherFee = Convert.ToDecimal(this.textBoxQT.Text);
            modelFeeBGTruePayTab.CSFee = Convert.ToDecimal(this.textBoxCS.Text);
            modelFeeBGTruePayTab.SMFee = Convert.ToDecimal(this.textBoxSM.Text);


            modelFeeBGTruePayTab.BGTGDFee = Convert.ToDecimal(this.textBoxTGDRecv.Text);
            modelFeeBGTruePayTab.BGBGFee = Convert.ToDecimal(this.textBoxBGRecv.Text);
            modelFeeBGTruePayTab.BGLGFee = Convert.ToDecimal(this.textBoxLGRecv.Text);
            modelFeeBGTruePayTab.BGGKJSFee = Convert.ToDecimal(this.textBoxGKJSRecv.Text);
            modelFeeBGTruePayTab.BGGCFee = Convert.ToDecimal(this.textBoxGCRecv.Text);
            modelFeeBGTruePayTab.BGCGFee = Convert.ToDecimal(this.textBoxCGRecv.Text);
            modelFeeBGTruePayTab.BGKAFee = Convert.ToDecimal(this.textBoxGPSRecv.Text);
            modelFeeBGTruePayTab.BGJYFee = Convert.ToDecimal(this.textBoxJYRecv.Text);
            modelFeeBGTruePayTab.BGOtherFee = Convert.ToDecimal(this.textBoxQTRecv.Text);
            modelFeeBGTruePayTab.BGCSFee = Convert.ToDecimal(this.textBoxCSRecv.Text);
            modelFeeBGTruePayTab.BGSMFee = Convert.ToDecimal(this.textBoxSMRecv.Text);

            modelFeeBGTruePayTab.JinBanPerson = this.textBoxJingBan.Text;
            modelFeeBGTruePayTab.RecvMonyePerson = this.textBoxRecvPersonS.Text;
       
            modelFeeBGTruePayTab.TruePayMonth = Convert.ToInt32(this.textBoxJTNY.Text,10);
            modelFeeBGTruePayTab.ShouldPayMonth = Convert.ToInt32(this.textBoxRecvNY.Text, 10);

            modelFeeBGTruePayTab.isChaGui = this.checkBoxCheckJinE.Checked;
            modelFeeBGTruePayTab.bitFiledReserv1 = this.checkBoxZJInvoice.Checked;

            modelFeeBGTruePayTab.InputDate = System.DateTime.Now.Date;
            modelFeeBGTruePayTab.TruePayDate = this.dateTimePickerTruePay.Value.Date;

            modelFeeBGTruePayTab.FHCGFee = Convert.ToDecimal(this.textFhcgfBox.Text);
            modelFeeBGTruePayTab.RCDDFee = Convert.ToDecimal(this.textRcddfBox.Text);

            modelFeeBGTruePayTab.BGFHCGFee = Convert.ToDecimal(this.textBGFhcgfBox.Text);
            modelFeeBGTruePayTab.BGRCDDFee = Convert.ToDecimal(this.textBGRcddfBox.Text);

            modelFeeBGTruePayTab.PubTruePayMoney = modelFeeBGTruePayTab.BGFee;
            modelFeeBGTruePayTab.PrivateTruePayMoney = modelFeeBGTruePayTab.TGDFee +
                modelFeeBGTruePayTab.GKJSFee +
                modelFeeBGTruePayTab.KAFee +
                modelFeeBGTruePayTab.CGFee +
                modelFeeBGTruePayTab.LGFee + modelFeeBGTruePayTab.JYFee +
                modelFeeBGTruePayTab.GCFee +
                modelFeeBGTruePayTab.SMFee +
                modelFeeBGTruePayTab.CSFee +
                modelFeeBGTruePayTab.FHCGFee +
                modelFeeBGTruePayTab.RCDDFee +
                modelFeeBGTruePayTab.OtherFee;
            m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTab);
            Frm_Main.Log2DB(this.Text, "修改", this.comboBoxContractNo.Text);
            MessageBox.Show("修改记录成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            UpdateControl();
           
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = this.dateTimePickerShouldRecvYearMonth.Value;
            int nYear = dt.Year;
            int nMonth = dt.Month;
            this.textBoxRecvNY.Text = nYear.ToString() + nMonth.ToString().PadLeft(2, '0'); 
        }

        private void checkBoxCheckJinE_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxCheckJinE.Checked)
            {
                string strYear = this.dateTimePickerShouldRecvYearMonth.Value.Year.ToString();
                string strMonth = this.dateTimePickerShouldRecvYearMonth.Value.Month.ToString().PadLeft(2,'0');
                this.textBoxRecvNY.Text = strYear + strMonth;

            }
        }

        private void textBoxGPS_TextChanged(object sender, EventArgs e)
        {
            this.textBoxGPSRecv.Text = this.textBoxGPS.Text;
        }

        private void textBoxCG_TextChanged(object sender, EventArgs e)
        {
            this.textBoxCGRecv.Text = this.textBoxCG.Text;
        }

        private void textBoxGC_TextChanged(object sender, EventArgs e)
        {
            this.textBoxGCRecv.Text = this.textBoxGC.Text;
        }

        private void textBoxSM_TextChanged(object sender, EventArgs e)
        {
            this.textBoxSMRecv.Text = this.textBoxSM.Text;
        }

        private void textBoxCS_TextChanged(object sender, EventArgs e)
        {
            this.textBoxCSRecv.Text = this.textBoxCS.Text;
        }

        private void textBoxQT_TextChanged(object sender, EventArgs e)
        {
            this.textBoxQTRecv.Text = this.textBoxQT.Text;
        }

        private void buttonReCompute_Click(object sender, EventArgs e)
        {
            ProcessBGFeeEx((int)this.comboBoxContractNo.SelectedValue, this.comboBoxContractNo.Text);
            UpdateControl();
        }

        private void dateTimePickerSB_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePickerTruePay_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = this.dateTimePickerTruePay.Value;
            int nYear = dt.Year;
            int nMonth = dt.Month;
            this.textBoxJTNY.Text = nYear.ToString() + nMonth.ToString().PadLeft(2, '0');
        }

  

        private void button1_Click(object sender, EventArgs e)
        {
            string str = this.comboBoxContractNo.SelectedValue.ToString();

            int nID = Convert.ToInt32(str, 10);
            DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab = new DGBY.Model.FeeBGTruePayTab();

            modelFeeBGTruePayTab.PaidanRegisterTabID = nID;
            modelFeeBGTruePayTab.ToPubBianHao = "";
            modelFeeBGTruePayTab.ToPrivateBianHao = "";
            modelFeeBGTruePayTab.SBDate = DateTime.Parse("1900-01-01");
            //modelFeeBGTruePayTab.TruePayDate = DateTime.Parse("1900-01-01");
            //modelFeeBGTruePayTab.InputDate = DateTime.Parse("1900-01-01"); 
            modelFeeBGTruePayTab.TruePayMonth = 0;
            modelFeeBGTruePayTab.ShouldPayMonth = 0;
            modelFeeBGTruePayTab.PrivateTruePayMoney = 0;
            modelFeeBGTruePayTab.PubTruePayMoney = 0;

            modelFeeBGTruePayTab.ToPubFormula = "";
            modelFeeBGTruePayTab.ToPrivateFormula = "";

            modelFeeBGTruePayTab.JinBanPerson = "";
            modelFeeBGTruePayTab.RecvMonyePerson = "";

            modelFeeBGTruePayTab.IsToRecvPayTab = 0;
            modelFeeBGTruePayTab.IsToFinCheckMoneyTab = 0;
            modelFeeBGTruePayTab.Note = "";

            modelFeeBGTruePayTab.bitFiledReserv1 = false;
            modelFeeBGTruePayTab.bitFiledReserv2 = false;

            modelFeeBGTruePayTab.stringFiledReserv1 = "";
            modelFeeBGTruePayTab.stringFiledReserv2 = "";
            modelFeeBGTruePayTab.stringFiledReserv3 = "";
            modelFeeBGTruePayTab.stringFiledReserv4 = "";

            modelFeeBGTruePayTab.intFiledReserv1 = 33;
            modelFeeBGTruePayTab.intFiledReserv2 = 10;
            modelFeeBGTruePayTab.intFiledReserv3 = 0;
            modelFeeBGTruePayTab.intFiledReserv4 = 0;
            DateTime dtNow = System.DateTime.Now.Date;
            modelFeeBGTruePayTab.datetimeFiledReserv1 = dtNow;
            modelFeeBGTruePayTab.datetimeFiledReserv2 = dtNow;

            modelFeeBGTruePayTab.decimalFiledReserv1 = 0;
            modelFeeBGTruePayTab.decimalFiledReserv2 = 0;
            modelFeeBGTruePayTab.decimalFiledReserv3 = 0;
            modelFeeBGTruePayTab.decimalFiledReserv4 = 0;
            // modelFeeBGTruePayTab.SjDate = System.DateTime.Now.Date;
            int nId = m_bllFeeBGTruePayTab.Add(modelFeeBGTruePayTab);
            Frm_Main.Log2DB(this.Text, "复制增加", this.comboBoxContractNo.Text);
            UpdateControl(nId);
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;

            m_nSelectIndex = iIndex;
            DisplaySelectTraGridValue(m_dsBGTruePay, m_nSelectIndex);   
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {

        }

        private void textFhcgfBox_TextChanged(object sender, EventArgs e)
        {
            textBGFhcgfBox.Text = textFhcgfBox.Text;
        }

        private void textRcddfBox_TextChanged(object sender, EventArgs e)
        {
            textBGRcddfBox.Text = textRcddfBox.Text;
        }

        private void textBoxQT_TextChanged_1(object sender, EventArgs e)
        {
            textBoxQTRecv.Text = textBoxQT.Text;
        }

        private void Frm_FeeBGTruePayInput_Activated(object sender, EventArgs e)
        {
            InitContractNoCombox();
        }

        private void InitContractNoCombox()
        {
            try
            {
                if (Frm_Main.m_nContractSelectValue == 127)
                    return;
                DataRow[] drs = m_dsPaidan.Tables[0].Select("PaidanRegisterTabID = '" + Frm_Main.m_nContractSelectValue.ToString() + "'");
                if (drs.Length > 0)
                    this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                else
                {
                    m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
                    if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
                    {
                        this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                        this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                        this.comboBoxContractNo.DisplayMember = "合同编号";
                        this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                        this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                    }
                }
            }
            catch (System.Exception ex)
            {

            }

        }

        private void Frm_FeeBGTruePayInput_Deactivate(object sender, EventArgs e)
        {
            try
            {
                Frm_Main.m_nContractSelectValue = (int)this.comboBoxContractNo.SelectedValue;

            }
            catch (System.Exception ex)
            {
                //  MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
    }
}
